package flc.ast.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.core.AttachPopupView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.FrameAdapter;
import flc.ast.bean.FrameBean;
import stark.common.basic.view.container.StkLinearLayout;

public class ColorDialog1 extends AttachPopupView implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, OnItemClickListener {

    private static final String COLOR_STRING_FORMAT = "%02x%02x%02x";
    private int red, green, blue;
    private List<FrameBean> mFrameBeanList;
    private int tmpFramePos;
    private FrameAdapter mFrameAdapter;
    private StkLinearLayout llDialogView;
    private StkLinearLayout llColorView;
    private EditText etColorContent;
    private ImageView ivColorConfirm;
    private SeekBar redSeekBar;
    private TextView redToolTip;
    private SeekBar greenSeekBar;
    private TextView greenToolTip;
    private SeekBar blueSeekBar;
    private TextView blueToolTip;
    private RecyclerView rvColor;


    public ColorDialog1(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_color;
    }

    protected void initView() {

        llDialogView = (StkLinearLayout) findViewById(R.id.llDialogView);
        llColorView = (StkLinearLayout) findViewById(R.id.llColorView);
        etColorContent = (EditText) findViewById(R.id.etColorContent);
        ivColorConfirm = (ImageView) findViewById(R.id.ivColorConfirm);
        redSeekBar = (SeekBar) findViewById(R.id.redSeekBar);
        redToolTip = (TextView) findViewById(R.id.redToolTip);
        greenSeekBar = (SeekBar) findViewById(R.id.greenSeekBar);
        greenToolTip = (TextView) findViewById(R.id.greenToolTip);
        blueSeekBar = (SeekBar) findViewById(R.id.blueSeekBar);
        blueToolTip = (TextView) findViewById(R.id.blueToolTip);
        rvColor = (RecyclerView) findViewById(R.id.rvColor);

        red = 255;
        green = 0;
        blue = 0;
        mFrameBeanList = new ArrayList<>();
        tmpFramePos = 0;

        redSeekBar.setProgress(red);
        greenSeekBar.setProgress(green);
        blueSeekBar.setProgress(blue);

        llColorView.setBackgroundColor(Color.rgb(red, green, blue));

        etColorContent.setText(String.format(COLOR_STRING_FORMAT, red, green, blue));
        etColorContent.clearFocus();

        ivColorConfirm.setOnClickListener(this);
        redSeekBar.setOnSeekBarChangeListener(this);
        greenSeekBar.setOnSeekBarChangeListener(this);
        blueSeekBar.setOnSeekBarChangeListener(this);
        etColorContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() == 6) {
                    llColorView.setBackgroundColor(Color.parseColor("#" + String.format(COLOR_STRING_FORMAT, red, green, blue)));
                }
            }
        });

        rvColor.setLayoutManager(new GridLayoutManager(getContext(), 8));
        mFrameAdapter = new FrameAdapter();
        rvColor.setAdapter(mFrameAdapter);
        mFrameAdapter.setOnItemClickListener(this);

        // 监听软键盘的显示和隐藏事件

        etColorContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override

            public void onGlobalLayout() {

                Rect r = new Rect();

                etColorContent.getWindowVisibleDisplayFrame(r);

                int screenHeight = etColorContent.getRootView().getHeight();

                int keypadHeight = screenHeight - r.bottom;

                if (keypadHeight > screenHeight * 0.15) { // 软键盘显示

                    // 将EditText顶起底部，避免被键盘遮挡

                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llDialogView.getLayoutParams();

                    params.bottomMargin = keypadHeight - ConvertUtils.dp2px(105);

                    llDialogView.setLayoutParams(params);

                } else { // 软键盘隐藏

                    // 将EditText恢复到原始位置

                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llDialogView.getLayoutParams();

                    params.bottomMargin = 0;

                    llDialogView.setLayoutParams(params);

                }

            }

        });

        getFrameData();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        FrameBean item = mFrameAdapter.getItem(position);
        mFrameAdapter.getItem(tmpFramePos).setSelected(false);
        item.setSelected(true);
        tmpFramePos = position;
        mFrameAdapter.notifyDataSetChanged();

        etColorContent.setText(item.getFrameColor().replace("#", ""));
        llColorView.setBackgroundColor(Color.parseColor(item.getFrameColor()));

        dismiss();
        if (listener != null) {
            listener.onClickConfirm(item.getFrameColor());
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivColorConfirm:
                String content = etColorContent.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.showShort(R.string.et_color_tips);
                    return;
                }
                if (content.length() < 6) {
                    ToastUtils.showShort(R.string.color_tips2);
                    return;
                }
                dismiss();
                if (listener != null) {
                    listener.onClickConfirm("#" + content);
                }
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        if (seekBar.getId() == R.id.redSeekBar) {
            red = progress;
            redToolTip.setText(red + "");
        } else if (seekBar.getId() == R.id.greenSeekBar) {
            green = progress;
            greenToolTip.setText(green + "");
        } else if (seekBar.getId() == R.id.blueSeekBar) {
            blue = progress;
            blueToolTip.setText(blue + "");
        }
        llColorView.setBackgroundColor(Color.rgb(red, green, blue));
        etColorContent.setText(String.format(COLOR_STRING_FORMAT, red, green, blue));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    private void getFrameData() {
        mFrameBeanList.add(new FrameBean("#FFFFFF"));
        mFrameBeanList.add(new FrameBean("#000000"));
        mFrameBeanList.add(new FrameBean("#FFDF8D"));
        mFrameBeanList.add(new FrameBean("#3A52BD"));
        mFrameBeanList.add(new FrameBean("#9BA9DE"));
        mFrameBeanList.add(new FrameBean("#00B8AB"));
        mFrameBeanList.add(new FrameBean("#63CDC3"));
        mFrameBeanList.add(new FrameBean("#CDA163"));
        mFrameBeanList.add(new FrameBean("#FF6159"));
        mFrameBeanList.add(new FrameBean("#FF5592"));
        mFrameBeanList.add(new FrameBean("#D990DE"));
        mFrameBeanList.add(new FrameBean("#8455C7"));
        mFrameBeanList.add(new FrameBean("#4C78FF"));
        mFrameBeanList.add(new FrameBean("#00E9FF"));
        mFrameBeanList.add(new FrameBean("#BEE29E"));
        mFrameBeanList.add(new FrameBean("#9AFFDA"));

        mFrameAdapter.setList(mFrameBeanList);
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        void onClickConfirm(String color);
    }
}
